package be.kuleuven.peno3.mobiletoledo.view.queue;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.Data.Client.QueueClient;
import be.kuleuven.peno3.mobiletoledo.model.QueueInfo;

public class QueueView extends Activity {
	private LinearLayout l;
	
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.queueviewlayout);
		
		TextView queueBar = (TextView) findViewById(R.id.queue_bar);
		queueBar.setText("Wachtrijen");
		l = (LinearLayout) findViewById(R.id.queueviewlayout);
		l.setPadding(0, 10, 0, 0);
		l.setGravity(Gravity.CENTER_HORIZONTAL);
		updateList();
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		updateList();
	}
	
	private void updateList() {
		l.removeAllViewsInLayout();
		
		Button b = new Button(this);
		b.setText(this.getResources().getString(R.string.insertqueuelengthbuttontext));
		b.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10);
		b.setTextColor(0xFF456807);
		ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(QueueView.this, InsertQueueLength.class);
				startActivity(i);
			}
		});
		l.addView(b, params);
		
		QueueInfo qi;TextView t;
		String[] services = this.getResources().getStringArray(R.array.services);
		
		for(int i=0;i<services.length;i++) {
			qi = QueueClient.getLength(services[i]);
			ImageView image = new ImageView(this.getApplicationContext());
			if(i == 0){
				image.setImageDrawable(getResources().getDrawable(R.drawable.line_full));
			}
			else{
				image.setImageDrawable(getResources().getDrawable(R.drawable.line));
			}
			image.setPadding(0, 5, 0, 5);
			l.addView(image);
			t=new TextView(this.getApplicationContext());
			t.setText(services[i]);
			t.setTextSize(TypedValue.COMPLEX_UNIT_PT, 12);
			t.setTextColor(0xFF69931e);
			t.setOnClickListener(new QueueClickListener());
			t.setTag(services[i]);
			l.addView(t);

			if(qi==null) {
				t=new TextView(this.getApplicationContext());
				t.setText("geen info beschikbaar");
				l.addView(t);
			}
			else {
				t=new TextView(this.getApplicationContext());
				t.setText("Wachtrij van "+qi.getLength()+" personen");
//				t.setOnClickListener(new QueueClickListener());
				t.setTag(services[i]);
				l.addView(t);
				t=new TextView(this.getApplicationContext());
				t.setTextSize((float) 10);
				t.setGravity(Gravity.RIGHT);
				Calendar cal = qi.getCal();
				String min = "" + cal.get(Calendar.MINUTE);
				if(min.length() == 1){
					min = "0" + min;
				}
				t.setText("laatste update op: "+cal.get(Calendar.DATE)+"/"+(cal.get(Calendar.MONTH)+1)+" om "+cal.get(Calendar.HOUR_OF_DAY)+":"+min);
//				t.setOnClickListener(new QueueClickListener());
				t.setTag(services[i]);
				l.addView(t);
			}
		}
	}
	
	private class QueueClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent i = new Intent(QueueView.this, HistoryView.class);
			i.putExtra("location", (String) v.getTag());
			onClickAnimation(v);
			activityStartDelay(i);
		}
	}
	
	private void onClickAnimation(View view){
		view.startAnimation(AnimationUtils.loadAnimation(this, R.layout.text_click));
	}
	
	private void activityStartDelay(Intent intent){
		int delay = 400; //Time to delay activity startup
		final Intent i = intent;
		final Handler handler = new Handler(); 
        Timer t = new Timer(); 
        t.schedule(new TimerTask() { 
        	public void run() { 
        		handler.post(new Runnable() { 
        			public void run() { 
        				startActivity(i); 
        			} 
        		}); 
        	} 
        }, delay);
	}
}
